package com.hxit.huaxiamelongarden.controller;

import com.hxit.huaxiamelongarden.mapper.UploadMapper;
import com.hxit.huaxiamelongarden.utils.AliOSSUtils;
import com.hxit.huaxiamelongarden.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * @ClassName UploadController
 * @Author 12694
 * @Date 2025/1/23 21:05
 * @Version 1.0
 **/
@Slf4j
@RestController
public class UploadController {
    @Autowired
    private UploadMapper uploadMapper;
    @Autowired
    private AliOSSUtils aliOSSUtils;

    @PostMapping("/upload")
    public Result upload(@RequestAttribute("userId") String userId, MultipartFile image) throws IOException {
        //调用阿里云OSS工具类，将上传上来的文件存入阿里云
        String url = aliOSSUtils.upload(image);
        log.info("上传图片的url:{}",url);
        //更新数据库头像信息
        uploadMapper.updateImage(Integer.parseInt(userId),url);
        //将图片上传完成后的url返回，用于浏览器回显展示
        return Result.success(url);
    }
}
